#ifndef BITMAPFONT_H
#define BITMAPFONT_H

#include "SpriteBatch.h"
#include "TextureManager.h"

namespace Cvekas {

/// Describes single BitmapFont character
struct CharDesc
{
	/// Source texure rectangle containing character image
	RectF source;
	/// Offset (in pixels) from cursor position to left corner of character
	float x_offset;
	/// Offset (in pixels) from cursor position to top corner of character
	float y_offset;
	/// Amount of pixels cursor must advance after printing character
	float x_advance;
};

/// Describes bitmap font
struct BitmapFontDesc
{
	/// Texture containing character images
	Texture2DPtr font_texture;
	/// Font height in pixels
	float line_height;
	/// Description of each character
	CharDesc chars[256];
};

/// Renders text using characters stored in a texture
class BitmapFont : public boost::noncopyable
{
public:
	/// Constructor. Loads font description from mml font description file.
	BitmapFont(SpriteBatchPtr sprite_batch, const std::string& path, uint layer = 0);
	/// Constructor. Requires to provide BitmapFontDesc.
	BitmapFont(SpriteBatchPtr sprite_batch, const BitmapFontDesc& desc, uint layer = 0);
	/// Destructor
	virtual ~BitmapFont();

	/// Draws text on screen
	/// \param position Coordinates of top left corner of text in virtual screen space (see SpriteBatch)
	/// \param text Text to draw
	/// \param color Color of text
	void draw(const Vector2& position, const std::string& text, const Color& color = Color(255, 255, 255, 255));

	/// Draws scaled text on screen
	/// \param center_position Coordinates of center of text in virtual screen space (see SpriteBatch)
	/// \param scale Scaling factor. 1.0f - normal size, 2.0f - double, etc.
	/// \param text Text to draw
	/// \param color Color of text
	void drawScaled(const Vector2& center_position, float scale, const std::string& text, const Color& color = Color(255, 255, 255, 255));
	
	/// Fits text into rectangle and draws it on screen
	/// \param rect Rectangle to fit text into. In virtual screen space (see SpriteBatch)
	/// \param text Text to draw
	/// \param color Color of text
	void drawToRect(const RectF& rect, const std::string& text, const Color& color = Color(255, 255, 255, 255));
	
	/// Returns width of text string in pixels
	float getWidth(const std::string& text) const;

	/// Returns height of single line of text in pixels
	float getHeight() const { return desc.line_height; }

private:
	BitmapFontDesc desc;
	SpriteBatchPtr sprite_batch;

	uint layer;
};

typedef boost::shared_ptr<BitmapFont> BitmapFontPtr;

} // namespace

#endif